package com.wwdx.service.impl;

import com.google.common.collect.Maps;
import com.wwdx.model.WwdxThemeModel;
import com.wwdx.service.WwdxThemeServiceI;
import com.wwdx.utils.ConstantHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author liguoliang
 */
@Service
public class WwdxThemeServiceImpl extends BaseServiceImpl<WwdxThemeModel> implements WwdxThemeServiceI {

    @Override
    public Long selectModelCount(WwdxThemeModel model) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(6);
        String hql = "select count(1) from WwdxThemeModel\t"+getParams(model, params);
        return count(hql, params);
    }

    private String getParams(WwdxThemeModel model, Map<String, Object> params){
        StringBuilder sb = new StringBuilder("where 1=1\t");
        String orderBy = "\tORDER BY sort desc";
        //主题类型查询
        Integer type = model.getType();
        if (type != null){
            sb.append("and type=:type\t");
            params.put("type",type);
        }
        //主题ID查询
        Long themeid = model.getThemeid();
        if (themeid != null){
            sb.append("and themeid =:themeid\t");
            params.put("themeid",themeid);
        }
        //主题名称查询
        String title = model.getTitle();
        if (StringUtils.isNotBlank(title)){
            sb.append("and title like :title\t");
            params.put("title","%"+title+"%");
        }
        //是否上线查询
        Integer status = model.getStatus();
        if (status != null){
            params.put("status",status);
            sb.append("and status=:status\t");
        }
        //是否首页显示查询
        Integer ishost = model.getIshost();
        if (ishost != null){
            orderBy = "\tORDER BY ishost,sort desc";
            params.put("ishost",ishost);
            sb.append("and ishost=:ishost\t");
        }
        //主题随机数量查询
        Boolean random = model.getRandom();
        if (random != null && random){
            orderBy = "\tORDER BY RAND()";
        }
        sb.append(orderBy);
        return sb.toString();
    }

    @Override
    public List<WwdxThemeModel> selectModelList(WwdxThemeModel model, Integer page, Integer rows) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(6);
        String hql = "from WwdxThemeModel\t"+getParams(model,params);
        return find(hql,params,page,rows);
    }

    @Override
    public WwdxThemeModel getByThemeId(Long themeid) {
        String key = String.format(ConstantHolder.THEME_OBJ, themeid);
        WwdxThemeModel wwdxThemeModel = (WwdxThemeModel) valueOperations.get(key);
        if (wwdxThemeModel == null){
            Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
            params.put("themeid",themeid);
            String hql = "from WwdxThemeModel where themeid=:themeid";
            wwdxThemeModel = getByHql(hql,params);
            if (wwdxThemeModel != null){
                valueOperations.set(key,wwdxThemeModel);
            }
        }
        return wwdxThemeModel;
    }
}